<?php
defined('IN_TS') or die('Access Denied.');
switch ($ts) {
    case 'autobirth':
        $arrGroup =  aac('group')->findAll('group','isplace=2 and status<>0'));
        foreach($arrGroup as $item){
            if($item['birth']&&date('m-d',$item['birth'])==date('m-d')){
                $age = date('Y')-date('Y',$item['birth']);
                //发私信
                //发送系统消息开始
                $msg_userid = '0';
                $msg_touserid = $item['userid'];
                $msg_content = '今天是'.$item['groupname'].$age.'岁生日，提醒一下，可以在群里跟小伙伴一起庆祝。';
                $msg_tourl = tsUrl('group','show',array('id'=>$item['groupid']));
                aac('message')->sendmsg($msg_userid,$msg_touserid,$msg_content,$msg_tourl);
                //发送系统消息end

                //发广播
                $content = '今天是<a href="'.tsUrl('group','show',array('id'=>$item['groupid'])).'">'.$item['groupname'].$age.'岁生日！</a>';
                aac('weibo')->create('weibo',array(
                    'userid'=>1,
                    'content'=>$content,
                    'isaudit'=>0,
                    'addtime'=>date('Y-m-d H:i:s'),
                    'uptime'=>date('Y-m-d H:i:s'),
                ));

            }
        }
        break;
    case 'setactive':
        $arrGroup = aac('group')->findAll('group',array('isplace'=>2));
        foreach($arrGroup as $item){
            $count_active = 0;
            $groupUsers = aac('group')->findAll('group_user',array('groupid'=>$item['groupid']),'userid');
            foreach($groupUsers as $v){
                $arrDuty = aac('duty')->find('duty','groupid='.$item['groupid'].' and status=1 and cateid=1 and userid='.$v['userid'].' and endtime>'.strtotime('-30 day'));
                if($arrDuty){
                    $count_active=$count_active+1;
                }
             }
            $count_lastduty = aac('duty')->findCount('duty','groupid='.$item['groupid'].' and status=1 and cateid=1  and endtime>'.strtotime('-7 day'));
            $count_nextduty = aac('duty')->findCount('duty','groupid='.$item['groupid'].' and cateid=1  and endtime>'.time());
            aac('group')->update('group',array('groupid'=>$item['groupid']),array('count_active'=>$count_active,'count_lastduty'=>$count_lastduty,'count_nextduty'=>$count_nextduty));
        }
        break;
    //连续180天未值班取消馆东资格
    case 'cancelstock':
        $arrGroupUser=aac('group')->findAll('group_user',array('isstock'=>2));
        foreach($arrGroupUser as $item){
            $arrDuty=aac('duty')->find('duty','userid='.$item['userid'].' and groupid='.$item['groupid'].' and status=1 and begintime>'.strtotime('-180 day'));
            if(!$arrDuty){
                $arrGroupUser=aac('group')->update('group_user',array('userid'=>$item['userid'],'groupid'=>$item['groupid'],'isstock'=>2),array('isstock'=>0));
                //更新用户信息
                $arrGroupUser=aac('group')->find('group_user','userid='.$item['userid'].' and isstock>0');
                if(!$arrGroupUser){
                    aac('user')->update('user_info',array('userid'=>$item['userid']),array('isstock'=>1));
                }
                $count_sto=aac('group')->findCount('group_user','(isstock=1 or isstock=2) and groupid='.$groupid);
                aac('group')->update('group',array(
                    'groupid'=>$groupid
                ),array(
                        'count_sto'=>$count_sto
                    )
                );

                //发私信通知
                $userid = '0';
                $touserid = $item['userid'];
                $arrGroup = aac('group')->find('group',array('groupid'=>$item['groupid']));
                $content = '您已经超过半年没有参加'.$arrGroup['groupname'].'值班了，根据本馆的章程，取消馆东身份，请知悉。如果给分馆做月捐或者未来半年累计服务5次，可以恢复馆东身份。';
                if($arrGroup['donateurl']){
                    $url =	$arrGroup['donateurl'];
                }else{
                    $url = 'http://lxi.me/c03pr';
                }
                aac('message')->sendmsg($userid,$touserid,$content,$url);

            }
        }
        break;
    //定期下架过期商品
    case 'updatestatus':
        $arrGift=aac('gift')->findAll('gift','status=0 and limittime<'.time());
        foreach($arrGift as $item){
            aac('gift')->update('gift',array(
                'giftid'=>$item['giftid'],
            ),array(
                'status'=>2
            ));
            if($item['cate']==0){
                $catename='志愿者福利';
            }else{
                $catename='义卖品';
            }
            //发私信通知
            $userid = '0';
            $touserid = $item['userid'];
            $content = '您发布的'.$catename.'超过5天无人问津，已经下架，仅您本人可见。您可以通过编辑商品信息，延长有效期恢复上架。建议您加强推广力度，以便早日售出。';
            $url = tsUrl('gift','show',array('id'=>$item['giftid']));
            aac('message')->sendmsg($userid,$touserid,$content,$url);

        }
        break;
    //删除超期未确认的馆东名单
    case 'timeoutstock':
        aac('group')-> update('group_user','isstock=-1 and donate=0 and uptime<'.strtotime('-14 day'),array('isstock'=>-2));
        aac('group')-> update('group_user','isstock=-1 and donate=1 and uptime<'.strtotime('-14 day'),array('isstock'=>1));
        break;
    //定时发送贺卡
    case 'sendcard':
        $strCard = aac('user')->findAll('user_card','status=0 and sendtime<'.time());
        foreach($strCard as $item){
            aac('user')->update('user_card',array('cardid'=>$item['cardid']),array('status'=>1));
            //发私信通知
            $userid = '0';
            $touserid = $item['touserid'];
            $strTouser = aac('user')->find('user_info',array('userid'=>$item['touserid']));
            $strUser =  aac('user')->find('user_info',array('userid'=>$item['userid']));
            $content = $strTouser['username'].'，你好！馆友<a href="'.tsUrl('user','duty',array('id'=>$strUser['userid'])).'">'.$strUser['username'].'</a>给你送了一张贺卡，请点击后面的链接查看。长按贺卡下方按钮可以保存为图片，你可以发到朋友圈秀一下哦。点击ta的名字可以访问个人主页，个人主页右上角小齿轮点击一下可以看到菜单上的发送贺卡选项，你懂的。';
            $url = tsUrl('user','card',array('cardid'=>$item['cardid']));
            aac('message')->sendmsg($userid,$touserid,$content,$url);
        }
        break;
    //检查过期的预约是否登记
    case 'checkorder':
        aac('duty')->delete('duty','status=-1 and endtime<'.strtotime('-3 days'));
        $strDuty = aac('duty')->findAll('duty','status=-1 and endtime<'.time());
        foreach($strDuty as $item){
            //发私信通知
            $userid = '0';
            $touserid = $item['userid'];
            $strTouser = aac('user')->find('user_info',array('userid'=>$item['userid']));
            $strGroup = aac('group')->find('group',array('groupid'=>$item['groupid']));
            $content = $strTouser['username'].'，你好！你在'.$strGroup['groupname'].'预约的服务已经过期，还没有登记服务，请及时登记。如果预约后没有去服务，请点击后面的链接前往预约页面删除预约！';
            $url = tsUrl('duty','order',array('groupid'=>$item['groupid']));
            aac('message')->sendmsg($userid,$touserid,$content,$url);
        }
        break;
    //检查过期的心愿是否达成
    case 'checkwish':
        $strWishUser = aac('wish')->findAll('wish_users','status=1 and uptime<'.strtotime('-7 days'));
        foreach($strWishUser as $item){
            //发私信通知
            $userid = '0';
            $strWish = aac('wish')->find('wish',array('wishid'=>$item['wishid']));
            $touserid = $strWish['userid'];
            $strTouser = aac('user')->find('user_info',array('userid'=>$strWish['userid']));
            $strDonater = aac('user')->find('user_info',array('userid'=>$item['userid']));
            $content = $strTouser['username'].'，你好！你发布的心愿'.$strWish['title'].'被'.$strDonater['username'].'认领，发货已超过7天，请确认是否收货，如果已经收货，请在心愿页面点击捐赠人姓名确认收货！';
            $url = tsUrl('wish','show',array('id'=>$item['wishid']));
            aac('message')->sendmsg($userid,$touserid,$content,$url);
        }
        break;
    //提醒馆长核定服务
    case 'checkverify':
        $strDuty = aac('duty')->findAll('duty','status=0 and endtime<'.strtotime('-6 hours'));
        foreach($strDuty as $item){
            //发私信通知
            $userid = '0';
            $strGroup = aac('group')->find('group',array('groupid'=>$item['groupid']));
            $toUserid = $strGroup['userid'];
            $strToUser = aac('user')->find('user_info',array('userid'=>$toUserid['userid']));
            $strUser = aac('user')->find('user_info',array('userid'=>$item['userid']));
            $content = $strToUser['username'].'，你好！'.$strUser['username'].'在你管理的'.$strGroup['groupname'].'提交服务登记还没有核定，请点击后面的链接前往核定！';
            $url = tsUrl('duty','index',array('groupid'=>$item['groupid'],'status'=>3));
            aac('message')->sendmsg($userid,$toUserid,$content,$url);
        }
        break;

}